package com.sportschain.app.homepage.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.sportschain.app.R;

public class CircleProgressView extends View {

    private int mProgress = 50;

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Shader mShader;
    private RectF mRectF;
    private int mDiameter;
    private int mHeight;
    private int mLineWidth;
    private int mDisabledColor;

    public CircleProgressView(Context context) {
        this(context, null);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData(context);
    }

    private void initData(Context context) {
        mLineWidth = ConvertUtils.dp2px(9);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mLineWidth);
        mDisabledColor = getResources().getColor(R.color.color_efefef);
        mPaint.setColor(mDisabledColor);

        mShader = new LinearGradient(ConvertUtils.dp2px(160), 0, 0, ConvertUtils.dp2px(160),
                getResources().getColor(R.color.color_e76d00), getResources().getColor(R.color.color_f2c40a),
                Shader.TileMode.CLAMP);

        mRectF = new RectF();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        mDiameter = mHeight - mLineWidth;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mRectF.set(mLineWidth / 2, mLineWidth / 2, mHeight - mLineWidth / 2, mHeight - mLineWidth / 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setShader(mShader);
        canvas.drawArc(mRectF, -90, 360 * mProgress / 100f, false, mPaint);

        mPaint.setShader(null);
        mPaint.setColor(mDisabledColor);
        canvas.drawArc(mRectF, 360 * mProgress / 100f - 90, 360 * (100 - mProgress) / 100f, false, mPaint);

    }
}
